package com.shxc.smarttrade.old.myview;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;

import com.wordplat.ikvstockchart.drawing.IDrawing;
import com.wordplat.ikvstockchart.entry.Entry;
import com.wordplat.ikvstockchart.entry.EntrySet;
import com.wordplat.ikvstockchart.entry.SizeColor;
import com.wordplat.ikvstockchart.render.AbstractRender;

/**
 * Created by jcdev1 on 2017/5/15.
 */

public class CurrentTimeDrawing implements IDrawing {
    private Paint candlePaint; // 蜡烛图画笔
    private Paint linePaint;//虚线图画笔
    private AbstractRender render;
    private int screenWidth;

    private float rectHeight;
    private float rectWidth;

    private float candleSpace = 0.1f; // entry 与 entry 之间的间隙，默认 0.1f (10%)
    private final RectF kLineRect = new RectF(); // K 线图显示区域

    public CurrentTimeDrawing(int screenWidth) {
        this.screenWidth = screenWidth;
    }

    @Override
    public void onInit(RectF contentRect, AbstractRender render) {
        this.render = render;
        this.rectHeight = contentRect.bottom;//contentRect.bottom:网格底部的Y坐标，即到屏幕顶部的距离
        this.rectWidth = contentRect.right;
        kLineRect.set(contentRect);

        final SizeColor sizeColor = render.getSizeColor();
        if (candlePaint == null) {
            candlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            candlePaint.setStyle(Paint.Style.STROKE);
            candlePaint.setStrokeWidth(sizeColor.getCandleBorderSize());
        }
        if (linePaint == null) {
            linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            linePaint.setStyle(Paint.Style.STROKE);
            linePaint.setStrokeWidth(2);
            linePaint.setColor(Color.rgb(16, 53, 177));
        }

    }

    @Override
    public void computePoint(int minIndex, int maxIndex, int currentIndex) {

    }

    @Override
    public void onComputeOver(Canvas canvas, int minIndex, int maxIndex, float minY, float maxY) {
        final EntrySet entrySet = render.getEntrySet();
        canvas.save();
        canvas.clipRect(kLineRect);

        if (maxIndex > 0) {
            float[] newCandleRectBuffer = new float[4];
            int entrySize = entrySet.getEntryList().size();
            Entry entrys = entrySet.getEntryList().get(entrySize - 1);

            newCandleRectBuffer[0] = entrySize - 1 + candleSpace;
            newCandleRectBuffer[2] = entrySize - candleSpace;
            newCandleRectBuffer[1] = entrys.getOpen();
            newCandleRectBuffer[3] = entrys.getClose();
            render.mapPoints(newCandleRectBuffer);

            if (newCandleRectBuffer[3] > rectHeight) {
                drawCurrentStateMap(canvas, entrys.getClose() + "", rectHeight);
            } else {
                drawCurrentStateMap(canvas, entrys.getClose() + "", newCandleRectBuffer[3]);
            }
        }
        canvas.restore();

    }

    /**
     * 标识最新股价图-横线
     *
     * @param canvas
     * @param text
     * @param yCoord
     */
    private void drawCurrentStateMap(Canvas canvas, String text, float yCoord) {
//        yCoord = yCoord + 20;//为了好看，做整体向下平易操作
        PathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
        linePaint.setPathEffect(effects);
        Path path1 = new Path();
        path1.moveTo(0, yCoord);//替换0-->(rectWidth - 200)
        path1.lineTo(screenWidth - 20, yCoord);
        canvas.drawPath(path1, linePaint);

//        RectF rf = new RectF(screenWidth - 100, yCoord - 20, screenWidth - 20, yCoord + 20);
        RectF rf = new RectF(20, yCoord - 20, 120, yCoord + 20);


        candlePaint.setStyle(Paint.Style.FILL);
        candlePaint.setColor(Color.rgb(16, 53, 177));
        canvas.drawRect(rf, candlePaint);

        candlePaint.setColor(Color.WHITE);
        candlePaint.setTextSize(16);
        canvas.drawText(text + "", 40, yCoord + 5, candlePaint);
    }

    @Override
    public void onDrawOver(Canvas canvas) {

    }
}
